#pragma  once
#include "list"
#include "Event.h"

template <class BaseInterface>
class TDispatcherImpl : public BaseInterface
{
public:
	TDispatcherImpl()
	{
	}
	virtual ~TDispatcherImpl() {}
	void DispatchEvent(int nEventID, Event_t &e)
	{
		std::list<IEventListener*>::iterator itListner = m_arrListner[nEventID].begin();
		while (itListner != m_arrListner[nEventID].end())
		{
			IEventListener *pListenr = (*itListner);
			pListenr->OnEvent(nEventID, e);
			itListner++;
		}
	}
	void AddEventListner(int nEventID, IEventListener *pListner)
	{
		bool bFind = false;
		std::list<IEventListener*>::iterator itListner = m_arrListner[nEventID].begin();
		while (itListner != m_arrListner[nEventID].end())
		{
			if (pListner == (*itListner))
			{
				bFind = true;
				break;
			}
			itListner++;
		}
		if (!bFind)
		{
			std::list<IEventListener*> &lst = m_arrListner[nEventID];
			lst.push_back(pListner);
		}
	}
	void RemoveEventListner(IEventListener *pListner)
	{
		for (int i = EVENT_ID_BEGIN + 1; i < EVENT_ID_END; i++)
		{
			std::list<IEventListener*>::iterator itListner = m_arrListner[i].begin();
			while (itListner != m_arrListner[i].end())
			{
				if (pListner == (*itListner))
				{
					itListner = m_arrListner[i].erase(itListner);
				}
				else
				{
					itListner++;
				}
			}
		}
	}
private:
	std::list<IEventListener *> m_arrListner[EVENT_ID_END];
};